import time


from selenium.webdriver.common.by import By
from pages.base_page import Page


class RoutingRulesPage(Page):
    ele_title = (By.XPATH, u"//h3[text()='Routing Rules']")
    ele_error = (By.XPATH, u'//div[@role="alert"]')

    ele_agent =(By.XPATH, u"//span[text()='Agent']")
    ele_route_to =(By.XPATH, u"//div[@data-formfield='simpleRoutingRouteToAgent']")
    ele_route_to_agent = (By.XPATH, u'//ul[@role="menu"]/li[1]')
    ele_priority =(By.XPATH, u"//div[@data-formfield='priorityForSimpleRouting']")

    ele_simple =(By.XPATH, u"//button[@value='simple']")
    ele_custom = (By.XPATH, u"//button[@value='customRules']")

    ele_new_rule = (By.XPATH, u'//span[text()="New Rule"]')

    # crate a new rule
    ele_new_routing_rule_name = (By.ID, u'form-routingConfig-table-routingRules-form-routingRule-input-name')
    ele_new_routing_rule_enable = (By.XPATH,
                                   u"//*[@id='form-routingConfig-table-routingRules-form-routingRule']//div[@data-formfield='isEnabled']//input[@name='isEnabled']")
    ele_agent3 = (By.XPATH, u"//div[@data-formfield='routeToType']/div/div/div/button[@value='agent']")
    ele_route_to3 = (By.XPATH, u"//div[@data-formfield='routeToAgent']")
    ele_route_to_agent3 =(By.XPATH, u"//ul[@role='menu']/li[1]")
    ele_priority3 = (By.XPATH, u"//div[text()='Normal']")
    ele_new_routing_rule_save = (By.XPATH, u"//span[text()='OK']")

    # delete a rule
    ele_delete_new_routing_rule = (By.XPATH, u"(//a[text()='autotest']/../../../td[@data-tag='operation']/div/button)[3]")

    ele_route_them_to =(By.XPATH, "//input[@value='routeToDepartmentorAgent']")
    ele_agent2 = (By.XPATH,u"//span[text()='Agent']")
    ele_route_to2 =(By.XPATH, u"//div[@data-formfield='noRuleMatchedRouteToAgent']")
    ele_priority2 =(By.XPATH, u"//div[@data-formfield='priorityWhenNoRuleMatched']")
    ele_route_to_agent2 =(By.XPATH, u"//ul[@role='menu']/li[1]")
    ele_redirect =(By.XPATH, u"//span[text()='redirect them to offline message window and forward their messages to']")


    ele_is_enable_feature_switch = (By.XPATH, u"//input[@name='isEnabled']")

    ele_routing_rules_save_button = (By.XPATH, u"//span[text()='Save']")

    # 如果没有SimpleRoute功能，则检查图片和unlock button
    ele_img = (By.XPATH, u'//img[@alt="Simple Routing"]')
    ele_unlock_this_feature = (By.XPATH, u'//span[text()="Unlock This Feature"]')

    # 如果没有RoutingRules功能，则检查图片和unlock button
    ele_title2 =(By.XPATH, u"//p[text()='Rule-Based Chat Routing']")
    ele_img2 =(By.XPATH, u"//img[@alt='Rule-Based Chat Allocation']")
    ele_unlock2 = (By.XPATH, u"//span[text()='Chat with us']")



    def __init__(self, driver, url, site_id):
        Page.__init__(self, driver, url)
        self.site_id = site_id
        self.url = url

    # this method should be called first
    def is_enable_feature_switch(self):
        fs = self.find_element(*self.ele_is_enable_feature_switch)
        if fs.get_attribute('value') == "false":
            self.click(self.ele_is_enable_feature_switch)
        else:
            pass

    def check_routing_rules_page_when_has_simple_rules_and_routing_rules_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_is_enable_feature_switch).is_enabled()
        self.find_element(*self.ele_simple).is_enabled()
        self.find_element(*self.ele_custom).is_enabled()
        self.click(self.ele_simple)
        self.click(self.ele_agent)
        self.find_element(*self.ele_route_to).is_enabled()
        self.find_element(*self.ele_priority).is_enabled()

    def check_save_button_when_has_simple_route_feature(self):
        self.click(self.ele_agent)
        self.click(self.ele_route_to)
        self.click(self.ele_route_to_agent)
        self.click(self.ele_routing_rules_save_button)

    def check_routing_rules_page_when_has_routing_rules_feature(self):
        self.click(self.ele_custom)
        self.click(self.ele_route_them_to)
        self.find_element(*self.ele_route_to2).is_enabled()
        self.find_element(*self.ele_redirect).is_enabled()
        # create
        self.click(self.ele_new_rule)
        self.input_text(self.ele_new_routing_rule_name, "autotest")
        self.click(self.ele_new_routing_rule_enable)
        self.click(self.ele_agent3)
        self.click(self.ele_route_to3)
        self.click(self.ele_route_to_agent3)
        self.click(self.ele_new_routing_rule_save)

        # delete
        time.sleep(3)
        self.click(self.ele_delete_new_routing_rule)

        self.click(self.ele_route_them_to)
        self.click(self.ele_agent2)
        self.click(self.ele_route_to2)
        self.click(self.ele_route_to_agent2)
        self.click(self.ele_routing_rules_save_button)





    def check_save_button_page_when_has_routing_rules_feature(self):
        self.click(self.ele_routing_rules_save_button)

    def check_routing_rules_page_when_has_simple_rules_and_no_routing_rules_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_is_enable_feature_switch).is_enabled()
        self.find_element(*self.ele_route_to).is_enabled()
        self.find_element(*self.ele_priority).is_enabled()

    def check_routing_rules_page_when_no_simple_route_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_img).is_enabled()
        self.find_element(*self.ele_unlock_this_feature).is_enabled()

    def check_routing_rules_page_when_no_routing_rules_feature(self):
        self.click(self.ele_title2)
        self.find_element(*self.ele_img2).is_enabled()
        self.find_element(*self.ele_unlock2).is_enabled()
